import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/controller/base_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_team/deposit_record_team_logic.dart';
import 'package:flutter_09_game/pages/deposit/deposit_record/deposit_record_team/deposit_record_team_view.dart';
import 'package:flutter_09_game/pages/promotion/promotion_commission/promotion_commission_logic.dart';
import 'package:flutter_09_game/pages/promotion/promotion_commission/promotion_commission_view.dart';
import 'package:flutter_09_game/pages/promotion/promotion_link/promotion_link_view.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/date_picker/custom_date_picker_sheet.dart';
import 'package:flutter_09_game/widgets/tab/common_tab_bar_page.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';

class PromotionLogic extends BaseController
    with GetSingleTickerProviderStateMixin {
  late TabController tabController;

  // 响应式tab索引，用于监听tab变化
  RxInt currentTabIndex = 0.obs;

  late List<CommonTabModel> tabData = [
    CommonTabModel(
      view: PromotionCommissionPage(),
      title: LocaleKeys.myCommission.tr,
    ),
    CommonTabModel(
      view: DepositRecordTeamPage(tag: '2',),
      title: LocaleKeys.performanceQuery.tr,
    ),
    CommonTabModel(
      view: PromotionLinkPage(),
      title: LocaleKeys.linkManagement.tr,
    ),
  ];

  @override
  void onInit() {
    super.onInit();
    // 初始化推广页数据
    tabController = TabController(length: tabData.length, vsync: this);
    // 添加tab变化监听器
    tabController.addListener(_tabListener);
    clog('Tab监听器已添加，初始tab索引: ${tabController.index}');
  }

  @override
  void onReady() {
    super.onReady();
    // 页面准备就绪时的操作
  }

  @override
  void onClose() {
    tabController.removeListener(_tabListener);
    tabController.dispose();
    super.onClose();
    // 页面关闭时的清理操作
  }

  /// tab变化监听器
  /// 监听TabController的index变化并更新响应式变量
  void _tabListener() {
    if (!tabController.indexIsChanging) {
      // 只在tab切换完成时触发，避免滑动过程中的多次触发
      int newIndex = tabController.index;
      if (newIndex != currentTabIndex.value) {
        currentTabIndex.value = newIndex;
        // onTabChanged(newIndex);
        clog('Tab切换到索引: $newIndex');
        // if (newIndex == 0) {
        //   if (Get.isRegistered<PromotionCommissionLogic>()) {
        //     Get.find<PromotionCommissionLogic>().refreshList();
        //   }
        // } else
        if (newIndex == 1) {
          if (Get.isRegistered<DepositRecordTeamLogic>()) {
            Get.find<DepositRecordTeamLogic>().updateOfCurrentLogic();
          }
        }
      }
    }
  }

  // 选择的日期
  Rx<DateTime?> selectedDate = Rx<DateTime?>(null);
  Rx<DatePeriod?> selectedDateRange = Rx<DatePeriod?>(null);

  /// 显示日期范围选择器
  showOptionDate() async {
    final result = await CustomDatePickerSheet.show(
      initialDateRange: selectedDateRange.value,
      firstDate: DateTime(2020),
      lastDate: DateTime(2030),
      isRangeSelection: true,
    );

    if (result != null && result is DatePeriod) {
      selectedDateRange.value = result;
      print('选择的日期范围: ${result.start} - ${result.end}');
      // 这里可以添加日期范围选择后的业务逻辑
    }
  }

  /// 显示日期范围选择器
  showDateRangePicker() async {
    final result = await CustomDatePickerSheet.show(
      initialDateRange: selectedDateRange.value,
      firstDate: DateTime(2020),
      lastDate: DateTime(2030),
      isRangeSelection: true,
    );

    if (result != null && result is DatePeriod) {
      selectedDateRange.value = result;
      print('选择的日期范围: ${result.start} - ${result.end}');
      // 这里可以添加日期范围选择后的业务逻辑
    }
  }
}
